import { login, logout } from '@/api/user'
import { Message } from 'element-ui'
const state = {
  userInfo: {},
  selectProject: {},
  isLogin: false,
  sceneData:null,
  categories:null
}
// actions
const actions = {
  loginAct({ commit }, data) {
    return new Promise((resolve, reject) => {
      login(data)
        .then((response) => {
          if (!response) {
            reject('登录失败！')
          }
          resolve(response)
          //console.log(response)

          commit('loginMuts', response)
          this.$router.push("/home");
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  logoutAct({ commit }) {
    logout().then((response) => {
      if (response['code'] === 200) {
        commit('logoutMuts')
      } else {
        Message(response['msg'])
      }
    })
  }
}

// mutations
const mutations = {
  loginMuts(state, { value }) {
    //console.log(value)

    state.userInfo = value
    state.isLogin = true
    const userInfo = JSON.stringify(state.userInfo)
    localStorage.setItem('userInfo', userInfo)
    localStorage.setItem('isLogin', state.isLogin)
  },
  sceneDataMuts(state, value) {
    state.sceneData = value
  },
  categoriesMuts(state, value) {
    state.categories = value
    console.log("*****");
    
  },
  resetMuts(state, value) {
    state.userInfo = value
    state.isLogin = true
    const userInfo = JSON.stringify(state.userInfo)
    localStorage.setItem('userInfo', userInfo)
    localStorage.setItem('isLogin', state.isLogin)
  },
  logoutMuts(state) {
    localStorage.clear()
    state.userInfo = {}
    state.isLogin = false
  },
  permissionsMuts(state, { data }) {
    state.permissions = data
  },

  chatShowMuts(state, data) {
    state.chatShow = data
  },
  selectProjectMuts(state, data) {
    state.selectProject = data;
    const selectProject = JSON.stringify(state.selectProject)
    localStorage.setItem('selectProject', selectProject)

  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
